using System;
using IRepository;
using IServices;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System.Collections.Generic;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Utils.Table;

namespace Services 
{
    public class Whr_courseServices : BaseServices<Whr_course>, IWhr_courseServices 
    {
        private readonly SqlSugarClient _client;
        private readonly ILogger<Whr_courseServices> _logger;
        public Whr_courseServices(SqlSugarClient client,ILogger<Whr_courseServices> logger, IWhr_courseRepository repository) : base(repository) 
        {
            _client = client;
            _logger = logger;
        }

        public (int, List<Whr_course>) PageList(Bootstrap.BootstrapParams bootstrap) 
        {
            var totalNumber = 0;
            var query = _client.Queryable<Whr_course>();
            if (bootstrap.search != null && bootstrap.search == "recommend")
            {               
                query.Where(s => s.IsRecommend == 1);
            }
            query.Where(s => s.Status == 1);
            List<Whr_course> result = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);

            return (totalNumber, result);
        }

        public (int, List<CourseDto>) CoursePageList(int userId, Bootstrap.BootstrapParams bootstrap) 
        {
            var totalNumber = 0;
            var query = _client.Queryable<Whr_course_apply, Whr_course>((a, c) => new object[]
             {
                JoinType.Left, a.CourseId == c.CourseId
             }).Where((a, c) => a.UserId == userId || bootstrap.Status != 0 && c.Status == bootstrap.Status)
             .Select((a, c) => new CourseDto 
             {
                UserId = a.UserId,
                CourseId = a.CourseId,
                CourseName = c.CourseName,
                Logo = c.Logo,
                SupportTimes = a.SupportTimes,
                Expire = a.Expire,
                Lat = c.Lat,
                Lng = c.Lng,
                Address = c.Address,
                ApplyStatus = a.Status,
                ViewNumbers = c.ViewNumbers,
                CourseStatus = c.Status,
                RegistrationDeadline = c.RegistrationDeadline,
                ObjectId = c.ObjectId,
             }).MergeTable();
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return (totalNumber, list);
        }
    }
}